package com.tanhua.manage.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.manage.exception.BusinessException;
import com.tanhua.manage.mapper.UserFreezeMapper;
import com.tanhua.manage.pojo.Admin;
import com.tanhua.manage.pojo.RoleOpreation;
import com.tanhua.manage.pojo.UserFreeze;
import com.tanhua.manage.util.UserThreadLocal;
import com.tanhua.manage.vo.UserFreezeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
@Slf4j
public class UserFreezeService extends ServiceImpl<UserFreezeMapper, UserFreeze> {

    private static final String CACHE_KEY_FREEZE_PREFIX = "FREEZE_";

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private RoleOpreationService roleOpreationService;

    /**
     * 获取用户冻结状态
     */
    public Boolean getFreezeStatusByUserId(Long userId) {
        String cacheKey = CACHE_KEY_FREEZE_PREFIX + userId;
        return redisTemplate.hasKey(cacheKey);
    }

    public Boolean freeze(UserFreezeVo vo) {

        if (ObjectUtil.isEmpty(vo.getUserId())) {
            throw new BusinessException("用户id不能为空");
        }

        UserFreeze oneUserFreeze = super.getOne(Wrappers.<UserFreeze>lambdaQuery().eq(UserFreeze::getUserId, vo.getId()));

        if (ObjectUtil.isNotEmpty(oneUserFreeze)) {
            throw new BusinessException("不能重复冻结用户");
        }

        UserFreeze userFreeze = BeanUtil.toBean(vo, UserFreeze.class);

        super.save(userFreeze);
        //将用户的冻结操作保存到数据库中
        RoleOpreation roleOpreation = new RoleOpreation();
        Admin admin = UserThreadLocal.get();
        roleOpreation.setRole(admin.getUsername());
        roleOpreation.setUserId(vo.getUserId());
        roleOpreation.setOpreation("冻结账户");
        String desc ="冻结用户["+vo.getUserId()+"]"+ vo.getReasonsForFreezing();
        roleOpreation.setDescribes(desc);
        this.roleOpreationService.saveRoleOpreation(roleOpreation);


        //将用户的冻结状态保存到redis中
        int days = 0;
        if (vo.getFreezingTime() == 1) {
            days = 3;
        } else if (vo.getFreezingTime() == 2) {
            days = 7;
        }

        String cacheKey = CACHE_KEY_FREEZE_PREFIX + vo.getUserId();
        if (days > 0) {
            this.redisTemplate.opsForValue().set(cacheKey, "ok", Duration.ofDays(days));
        }else{
            this.redisTemplate.opsForValue().set(cacheKey, "ok");
        }

        return true;
    }

    public Boolean unfreeze(UserFreezeVo freezeVo) {
        if (ObjectUtil.isEmpty(freezeVo.getUserId())) {
            throw new BusinessException("用户id不能为空！");
        }

        //删除数据库中的数据
        super.remove(Wrappers.<UserFreeze>lambdaQuery().eq(UserFreeze::getUserId, freezeVo.getUserId()));

        //删除redis中的数据
        String cacheKey = CACHE_KEY_FREEZE_PREFIX + freezeVo.getUserId();
        this.redisTemplate.delete(cacheKey);

        //TODO 由于解冻原因不进行展现，所以就不保存数据了
        //将用户的解除冻结操作保存到数据库中
        RoleOpreation roleOpreation = new RoleOpreation();
        Admin admin = UserThreadLocal.get();
        roleOpreation.setRole(admin.getUsername());
        roleOpreation.setUserId(freezeVo.getUserId());
        roleOpreation.setOpreation("解除冻结");
        String desc ="解除用户["+freezeVo.getUserId()+"]的冻结"+ freezeVo.getReasonsForThawing();
        roleOpreation.setDescribes(desc);
        this.roleOpreationService.saveRoleOpreation(roleOpreation);
        return true;
    }
}
